package com.nanyang.tms.controller.receipt;

import java.util.List;

import javax.validation.Valid;

import com.nanyang.tms.api.dto.request.receipt.*;
import com.nanyang.tms.api.dto.response.receipt.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.service.receipt.ReceiptBillRpcService;

@RestController
@RequestMapping("/receipt")
public class ReceiptBillController {
    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private ReceiptBillRpcService receiptRpcService;
    /**
     * <p>listReceiptBill</p>
     * <p>Description:查询收货单列表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<ReceiptBillPageResponse>> listReceiptBill(@RequestBody ReceiptBillQueryRequest param) {
        ApiResult<PageBean<ReceiptBillPageResponse>> apiResult = receiptRpcService.listReceiptBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>listReceiptBill</p>
     * <p>Description:模糊查询收货单列表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/listFuzzy", method = RequestMethod.POST)
    public ApiResult<PageBean<ReceiptBillFuzzyPageResponse>> listReceiptBill(@RequestBody ReceiptBillFuzzyQueryRequest param) {
        ApiResult<PageBean<ReceiptBillFuzzyPageResponse>> apiResult = receiptRpcService.listFuzzyReceiptBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listReceiptBill</p>
     * <p>Description:查询根据编码</p>
     *
     * @param receiptCode
     * @return
     */
    @RequestMapping(value = "/selectByCode/{receiptCode}", method = RequestMethod.GET)
    public ApiResult<List<ReceiptBillDetailResponse>> selectByCode(@PathVariable("receiptCode") String receiptCode) {
        ApiResult<List<ReceiptBillDetailResponse>> apiResult = receiptRpcService.selectByCode(receiptCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 添加
     * @param param
     * @return
     */

    @BindParam
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ApiResult<Boolean> saveReceiptBill(@BindParam @RequestBody @Valid ReceiptBillAddRequest param) {
        ApiResult<Boolean> apiResult = receiptRpcService.saveReceiptBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 设置完成
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    public ApiResult<Boolean> updateState(@BindParam @RequestBody @Valid ReceiptBillUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = receiptRpcService.updateState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 作废
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/cancelState", method = RequestMethod.POST)
    public ApiResult<Boolean> cancelState(@BindParam @RequestBody @Valid ReceiptBillUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = receiptRpcService.cancelState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>listReceiptBill</p>
     * <p>Description:前台-查询收货单列表</p>
     *
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/listReceiptBill", method = RequestMethod.POST)
    public ApiResult<PageBean<ReceiptBillFrontPageResponse>> listFrontReceiptBill(@BindParam @RequestBody ReceiptBillCustomerQueryRequest param) {
        ApiResult<PageBean<ReceiptBillFrontPageResponse>> apiResult = receiptRpcService.listFrontReceiptBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>listReceiptBill</p>
     * <p>Description:前台-查询根据编码</p>
     *
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ApiResult<List<ReceiptBillFrontDetailResponse>> selectFrontByCode(@BindParam @RequestBody ReceiptBillFrontDetailRequest param) {
        ApiResult<List<ReceiptBillFrontDetailResponse>> apiResult = receiptRpcService.selectFrontByCode(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
